package fun.zhufn.oldcomm.model.bo.customlist;

import fun.zhufn.oldcomm.model.bo.json.JsonList;
import fun.zhufn.oldcomm.model.bo.json.JsonMap;
import fun.zhufn.oldcomm.model.bo.User;

import java.util.logging.Logger;

public class UserList extends CustomList<User>{
    private static final Logger log = Logger.getLogger(UserList.class.toString());
    @Override
    public JsonList toJsonList() {
        JsonList ret = new JsonList();
        for (User u: data) {
            JsonMap mp = new JsonMap();
            mp.put("account", u.getAccount());
            mp.put("pwd", u.getPwd());
            mp.put("name", u.getName());
            mp.put("birthday", u.getBirthday());
            mp.put("phone", u.getPhone());
            mp.put("isMale", u.isMale());
            mp.put("permission", u.permission);
            ret.add(mp);
        }
        return ret;
    }

    @Override
    public void fromJsonList(JsonList d) {
        if (d == null) return;
        data.clear();
        for (Object o: d) {
            try {
                JsonMap v = (JsonMap) o;
                data.add(new User(
                        (String)v.get("account"),
                        (String)v.get("pwd"),
                        (String)v.get("name"),
                        (String)v.get("birthday"),
                        (String)v.get("phone"),
                        (Boolean)v.get("isMale"),
                        (int)v.get("permission")
                ));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
